#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QFileDialog>
#include <QWidget>
#include <QVBoxLayout>
#include <QTimer>
#include <QFile>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 设置窗口样式
    this->setMainStyle();

    // 创建一个媒体播放器
    this->player = new QMediaPlayer;

    // 创建一个视频小部件
    this->videoWidget = new QVideoWidget;

    // 布局
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(videoWidget);
    ui->widget->setLayout(layout);

    // 连接视频小部件和媒体播放器
    player->setVideoOutput(videoWidget);

    connect(ui->actionOpenFile, &QAction::triggered, [this](){
       // 打开文件对话框选择视频文件
       QString videoFile = QFileDialog::getOpenFileName(this, "打开视频", "D:/前端课程/02-css/css-3/视频", tr("*.mp4 *.avi *.wmv"));
       if (!videoFile.isEmpty()) {
            // 加载并播放视频文件
            player->setMedia(QMediaContent(QUrl::fromLocalFile(videoFile)));
       }
    });



    this->timer = new QTimer(this);
    connect(timer, &QTimer::timeout, [this](){
        // 每秒自增1
        this->nowTime++;
        // 同步播放进度条
        ui->horizontalSlider->setValue(this->nowTime);

        // 格式化当前时间进度并设置
        QString tmp = this->formatTime(this->nowTime);
        ui->playTimeLabel->setText(tmp);
    });

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_playBtn_clicked()
{
    if (this->isPlay == false)
    {
        // 播放视频
        this->player->play();
        // 修改按钮图片
        ui->playBtn->setIcon(QIcon(":/images/pause.png"));

        // 初始化当前时间为
        ui->playTimeLabel->setText("00:00:00");

        // 设置总时长标签
        this->totalTime = this->player->duration() / 1000;
        QString tmp = this->formatTime(this->totalTime);
        ui->totalTimeLabel->setText(tmp);

        // 设置时间进度条为总秒数
        ui->horizontalSlider->setMaximum(this->totalTime);

        // 开启定时器，每秒调整时间进度条
        timer->start(1000);
    }
    else
    {
        // 暂停
        this->player->pause();
        ui->playBtn->setIcon(QIcon(":/images/play.png"));

        timer->stop();
    }

    this->isPlay = !this->isPlay;
}



void MainWindow::on_horizontalSlider_valueChanged(int value)
{
    this->nowTime = value;
}

void MainWindow::on_horizontalSlider_sliderPressed()
{
    this->player->stop();
    this->timer->stop();
}

void MainWindow::on_horizontalSlider_sliderReleased()
{
    this->player->setPosition(this->nowTime * 1000);
    this->player->play();
    this->timer->start();
}

// 设置样式
void MainWindow::setMainStyle()
{
    QFile file(":/qss/main.qss");
    file.open(QIODevice::ReadOnly);
    QString style = file.readAll();
    this->setStyleSheet(style);
}

// 将秒数格式化为 hh:mm:ss
QString MainWindow::formatTime(int seconds)
{
    QVariant h = seconds / 3600;
    h = h.toInt() < 10 ? "0" + h.toString() : h;
    QVariant m = seconds % 3600 / 60;
    m = m.toInt() < 10 ? "0" + m.toString() : m;
    QVariant s = seconds % 60;
    s = s.toInt() < 10 ? "0" + s.toString() : s;

    return QString("%1:%2:%3").arg(h.toString()).arg(m.toString()).arg(s.toString());
}
